var sjzdsy = {
    setBtnStatus:function () {
        var row = $('#grid_sjzdsy').datagrid('getSelected');
        if (row && row.id) {
            $("#btn_sjzdsy_mod").show();
            $("#btn_sjzdsy_del").show();
        } else {
            $("#btn_sjzdsy_mod").hide();
            $("#btn_sjzdsy_del").hide();
        }
    }
}

$(function () {
    //表格初始加载
    $('#grid_sjzdsy').datagrid({
        url:$.webPath() +"/base/sjzdsy/sjzdsyList",
        toolbar:"#toolbar_sjzdsy",
        pagination:true,
        rownumbers:true,
        fitColumns:true,
        pageNumber: 1,
        singleSelect:true,
        fit:true,
        striped:true,
        columns:[[
            {field:'mc', title:'名称', width:40},
            {field:'bm', title:'编码', width:40},
            {field:'lx', title:'类型', width:40,
                formatter:function (value) {
                    if (value == '0')
                        return '系统类型';
                    if (value == '1')
                        return '合同文本';
                }
            },
            {field:'bz', title:'备注', width:200}
        ]],
        onLoadSuccess:sjzdsy.setBtnStatus,
        onClickRow:sjzdsy.setBtnStatus ,
      /*  rowStyler:function(index,row){
            if(row.lx == '0') {
                return 'background-color:rgba(205,201,201,0.64);';
            }
        },*/
        onDblClickCell:function (index, field) {
            if (field == "mc") {
                sjzdsy_modify('修改', 'update_sjzdsy');
            }
        }
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_sjzdsy_add").click(function () {
        sjzdsy_add('新增', 'add_sjzdsy');
    });
    //修改按钮
    $("#btn_sjzdsy_mod").click(function () {
        sjzdsy_modify('修改', 'update_sjzdsy');
    });
    //删除按钮
    $("#btn_sjzdsy_del").click(function () {
        sjzdsy_delete();
    });
    //查询按钮
    $("#btn_sjzdsy_search").click(function () {
        sjzdsy_search();
    });

});


//新增
function sjzdsy_add(title, action) {
    var href = $.webPath() + "/base/sjzdsy/sjzdsy_content";
    var actionUrl = $.webPath() + "/base/sjzdsy/" + action;
    sjzdsy_showDialog(title, href, actionUrl);

}

//修改
function sjzdsy_modify(title, action) {
    var record = $("#grid_sjzdsy").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "info");
        return;
    }
    var href = $.webPath() + "/base/sjzdsy/sjzdsy_content?sjzdsyId=" + record.id;
    var actionUrl = $.webPath() + "/base/sjzdsy/" + action;
    sjzdsy_showDialog(title, href, actionUrl);
}

//弹出新增或修改的窗口
function sjzdsy_showDialog(title, href, actionUrl) {
    var win = $.window({
        onBeforeClose:function(){
            $(".validatebox-tip").remove();
        },
        title:title,
        width:750,
        height:450,
        cache:false,
        url:href,
        iconCls: 'icon-add',
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    var valid = $('#form_sjzdsy').form('validate');
                    if(valid){
                        sjzdsy_search();
                    }else{
                      //  $.messager.progress('close');
                        $(win).mask('hide');
                   var r=$('#form_sjzdsy #sjzdz_grid').edatagrid("getSelected");
                   var index = $('#form_sjzdsy #sjzdz_grid').edatagrid("getRowIndex", r);
                   var vald=$('#sjzdz_grid').datagrid('validateRow',index);
                    if(vald){
                        $("#form_sjzdsy .tabs-title :eq(0)").click();
                    }else{
                        $("#form_sjzdsy .tabs-title :eq(1)").click();
                    }
                        return;
                    }

                   // $.messager.progress();
                     $(win).mask();
                    $('#sjzdz_grid').datagrid('acceptChanges');
                    var sjzdzRows = $('#sjzdz_grid').datagrid('getRows');
                    if(sjzdzRows !=null){
                        //收集数据字典值的信息在sjzdsyForm表单添加隐藏域
                        $('#form_sjzdsy').remove("[name='sjzdz.id']");
                        $('#form_sjzdsy').remove("[name='sjzdz.z']");
                          $('#form_sjzdsy').remove("[name='sjzdz.bm']");
                        $('#form_sjzdsy').remove("[name='sjzdz.bz']");
                        $.each(sjzdzRows,function (i, val){
                            $('#form_sjzdsy').append('<input name="sjzdsy.glz[' + i + '].id" type="hidden" value="' + val.id + '"/>');
                            $('#form_sjzdsy').append('<input name="sjzdsy.glz[' + i + '].z" type="hidden" value="' + val.z + '"/>');
                           $('#form_sjzdsy').append('<input name="sjzdsy.glz[' + i + '].bm" type="hidden" value="' + val.bm + '"/>');
                            $('#form_sjzdsy').append('<input name="sjzdsy.glz[' + i + '].bz" type="hidden" value="' + val.bz + '"/>');
                        })
                    }

                    $('#form_sjzdsy').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {

                          //  var isValid = $(this).form('validate');
                            //if (!isValid) {
                              //  $.messager.progress('close');
                               // $("#form_sjzdsy .tabs-title :eq(0)").click();
                            //}
                            //return isValid;

                        },
                        success:function (data) {
                            //$.messager.progress('close');
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_sjzdsy').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除数据字典
function sjzdsy_delete() {
    var row = $('#grid_sjzdsy').datagrid('getSelected');
    if (row) {
        $.messager.confirm('确认', '是否确认删除？', function (r) {
            if (r) {
                var index = $('#grid_sjzdsy').datagrid('getRowIndex', row);
                $.ajax({
                 url:$.webPath()+'/base/sjzdsy/delete_sjzdsy',
                        dataType: 'json',
                        type: 'POST',
                        data: {sjzdsyId:row.id},
                        beforeSend: function (){
                            $("#toolbar_sjzdsy").mask();
                        },
                    success:function(data){
                        $("#toolbar_sjzdsy").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_sjzdsy').datagrid('deleteRow', index);
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        }
                    }
                });
                //$.post($.webPath() + '/base/sjzdsy/delete_sjzdsy', {sjzdsyId:row.id}, function (data) {
                  //  var obj = eval('(' + data + ')');
                   /// if (obj.statusCode == '200') {
                     //   $('#grid_sjzdsy').datagrid('deleteRow', index);
                     //   $.messager.show({
                      //      title:'提示',
                       //     msg:obj.message,
                          //  timeout:3000,
                        //    showType:'slide'
                       // });
                    } else {
                        $.messager.alert("提示", obj.message, "error");
                    }
                });
    } else {
        $.messager.alert("提示", "请选择要删除数据字典索引！", "warning");
    }
}

//查询数据字典
function sjzdsy_search() {
    $('#grid_sjzdsy').datagrid('load',
        {
            mc:$('#sjzdsy_mc').val(),
            bm:$('#sjzdsy_bm').val()
        }
    );
}
